<?php

namespace App\Console\Commands;

use App\Models\Currency;
use App\Models\CurrencyLog;
use App\Models\Order;
use Illuminate\Console\Command;

class StatisticsOrder extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'statistics:order';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'statistics order';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 订单计划任务
     *
     * @ author: YANGGANG
     * @ date: 2018/3/28
     * @access public
     * @param  mixed    name    comment
     * @param  int    name    comment
     * @param  string    name    comment
     * @param  bool       name    comment
     * @param  array   name    comment
     * @return void|int|string|boolean|array        comment
     */
    public function handle()
    {
        $data_log = [];//记录
        $time = strtotime(date('Y-m-d')); //零点时间

        //开始时间和结束时间
        $end_date = date('Y-m-d H:i');
        $start_date = date('Y-m-d H:i', strtotime($end_date)-60);
        //get Data
        $minute_ident = (strtotime($end_date)-$time)/60; //零点到当前的分钟
        if ($minute_ident == 0){ //如果到了第二天零点 重新取前一天的时间戳
            $minute_ident = 1440;
            $time = $time - 24*60*60;
        }
        $currenct_ids = Currency::pluck('id');


        /**
         * 有订单就查订单 没有订单就查上一条 上一条没有就说明是第一期 直接取官方建议价
         */
        $datas = [];
        foreach ($currenct_ids as $item) {
            $id = $item;
            $close_price=0;
            //查询订单
            $order_list = Order::where(['cid'=>$id,'type'=>1])->whereBetween('created_at', [$start_date,$end_date])->get();

            //查询上一条
            $where['cid'] = $id;
            if($minute_ident - 1 == 0){
                $where['time'] = $time - 24*60*60;
                $where['minute_ident'] = 1440;
            }else{
                $where['time'] = $time;
                $where['minute_ident'] = $minute_ident-1;
            }
            $logo = CurrencyLog::where($where)->first();
            if ($order_list->isNotEmpty()){
                $max = $order_list->max('price');
                $min = $order_list->min('price');
                $amount = $order_list->sum('num');
                $last_order =$order_list->last();
                if (!$logo){
                    $close_price = Currency::where(['id'=>$id])->value('official_price');
                }else{
                    $close_price = $logo->close_price;
                }
                if ($last_order){
                    $close_price = $last_order['price']; //这一分钟内的最后一笔交易的价格为收盘价
                }
                $data = [
                    'cid'=>$id,
                    'open_price'=>$logo->close_price,
                    'close_price'=>$close_price,
                    'lower_price'=>$min,
                    'high_price'=>$max,
                    'time'=>$time,
                    'amount'=>$amount,
                    'minute_ident'=>$minute_ident,
                ];

            }else{
                if(!$logo){
                    //如果是第一条记录,则取官方指导价表
                    $official_price = Currency::where(['id'=>$id])->value('official_price');
                    $data = [
                        'cid'=>$id,
                        'open_price'=>$official_price,
                        'close_price'=>$official_price,
                        'lower_price'=>$official_price,
                        'high_price'=>$official_price,
                        'time'=>$time,
                        'amount'=>0,
                        'minute_ident'=>$minute_ident,
                    ];
                }else{
                    $data = [
                        'cid'=>$id,
                        'open_price'=>$logo->close_price,
                        'close_price'=>$logo->close_price,
                        'lower_price'=>$logo->close_price,
                        'high_price'=>$logo->close_price,
                        'time'=>$time,
                        'amount'=>0,
                        'minute_ident'=>$minute_ident,
                    ];
                }

            }
            $datas[] = $data;
        }
        (new CurrencyLog())->getConnection()->transaction(function()use($datas){
            foreach($datas as $item){
                (new CurrencyLog())->firstOrCreate($item);
            }
        });

    }
}
